package com.njtoyo.taxi.admin.rest.controller.business.geo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.njtoyo.taxi.admin.aop.PassToken;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.mapper.master.entity.GeoCanton;
import com.njtoyo.taxi.admin.rest.presenter.business.geo.RegionListPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.area.CantonQueryWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.canton.CantonCreateWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.canton.CantonUpdateWrapper;
import com.njtoyo.taxi.admin.service.business.geo.CantonService;
import com.taxi.entity.common.RestResult;
import com.njtoyo.taxi.entity.backend.AdminUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@RestController
@Api(value = "行政区", tags = "行政区")
@RequestMapping("business/geo/canton")
public class CantonController {

    @Autowired
    private CantonService cantonService;

    @GetMapping("/all")
    @ApiOperation(value = "全部行政区", notes = "只返回账号下的数据，无接口权限校验")
    RestResult getAll(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser) {
        return cantonService.getAll(adminUser);
    }

    @PostMapping
    @ApiOperation(value = "添加行政区", notes = "权限：canton_create")
    @PermissionScope(names = {"canton_create"})
    RestResult create(@Valid @RequestBody CantonCreateWrapper wrapper) {
        return cantonService.create(wrapper);
    }

    @GetMapping()
    @ApiOperation(value = "行政区列表", notes = "地区列表：权限：canton_list")
    @PermissionScope(names = {"canton_list"})
    RestResult<IPage<RegionListPresenter>> getList(CantonQueryWrapper wrapper) {
        return cantonService.getList(wrapper);
    }

    @PatchMapping("/{id}")
    @ApiOperation(value = "修改行政区", notes = "权限：canton_update")
    @PermissionScope(names = {"canton_update"})
    RestResult<Boolean> update(@PathVariable Long id, @RequestBody CantonUpdateWrapper wrapper) {
        return cantonService.update(id, wrapper);
    }


    @GetMapping("/listAll")
    @PassToken
    RestResult listAll() {
        return cantonService.listAll();
    }

    /**
     * 根据行政区id查询行政区
     *
     * @param cantonId
     * @return
     */
    @GetMapping("/getInfoById")
    RestResult getInfo(@RequestParam(value = "cantonId") Long cantonId) {
        GeoCanton infoById = cantonService.getInfoById(cantonId);
        return RestResult.success(infoById);
    }
}
